// ==UserScript==
// @name         拼多多接口数据自动生成工具
// @namespace    https://mms.pinduoduo.com/
// @version      0.3
// @description  try to take over the world!
// @author       Xiao Niu
// @match        https://mms.pinduoduo.com/sycm/stores_data/operation
// @icon         https://commimg.pddpic.com/mms_static/2019-06-20/a3b5eebb-2ed9-4d4a-b888-ac2f4892f363.png
// @updateURL    https://gitee.com/hankewins/monkey-scripts/raw/master/pinduoduo-mock-tool.user.js
// @downloadURL  https://gitee.com/hankewins/monkey-scripts/raw/master/pinduoduo-mock-tool.user.js
// @grant        unsafeWindow
// @grant        GM_registerMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_log
// @run-at       document-start
// ==/UserScript==

(function () {
  "use strict";
  const pddMockSwitch = GM_getValue("pdd-mock-switch");
  const originFetch = fetch;

  const pddMockSwitchHandler = () => {
    GM_setValue("pdd-mock-switch", !pddMockSwitch);
    unsafeWindow.location.reload();
  };

  GM_registerMenuCommand(
    pddMockSwitch ? "关闭-模拟拼多多接口数据" : "开启-模拟拼多多接口数据",
    pddMockSwitchHandler
  );

  const mockQueryMallDataPagePayList = (url, ...params) => {
    return originFetch(url, ...params).then(async (response) => {
      var res = await response.json();
      const radixNum = 5;
      if (res.success) {
        Object.assign(res.result, {
          curPayOrdrAmt: Number(res.result.curPayOrdrAmt * radixNum).toFixed(2),
          lastPayOrdrAmt: Number(res.result.lastPayOrdrAmt * radixNum).toFixed(
            2
          ),
          curPayOrdrUsrCnt: Number(
            res.result.curPayOrdrUsrCnt * radixNum
          ).toFixed(0),
          lastPayOrdrUsrCnt: Number(
            res.result.lastPayOrdrUsrCnt * radixNum
          ).toFixed(0),
          curPayOrdrCnt: Number(res.result.curPayOrdrCnt * radixNum).toFixed(0),
          lastPayOrdrCnt: Number(res.result.lastPayOrdrCnt * radixNum).toFixed(
            0
          ),
        });

        res.result.payList.map((item) => {
          if (item.curPayOrdrAmt) {
            item.curPayOrdrAmt = item.curCfmOrdrAmt * radixNum;
          }
          if (item.lastPayOrdrAmt) {
            item.lastPayOrdrAmt = item.lastPayOrdrAmt * radixNum;
          }
          return item;
        });
      }
      var blob = new Blob([JSON.stringify(res, null, 2)], {
        type: "application/json",
      });
      var init = { status: 200 };
      return new Response(blob, init);
    });
  };

  const mockQueryMallDataPageOverviewList = (url, ...params) => {
    return originFetch(url, ...params).then(async (response) => {
      var res = await response.json();
      const radixNum = 2.5;
      if (res.success) {
        res.result.map((item) => {
          item.payOrdrAmt = item.payOrdrAmt * radixNum;
        });
        var blob = new Blob([JSON.stringify(res, null, 2)], {
          type: "application/json",
        });
        var init = { status: 200 };
        return new Response(blob, init);
      }
    });
  };

  const mockQueryMallDataPageOverview = (url, ...params) => {
    return originFetch(url, ...params).then(async (response) => {
      var res = await response.json();
      const radixNum = 2.5;
      if (res.success) {
        Object.assign(res.result, {
          // 支付金额
          payOrdrAmt: Number(
            Number(434727.07 * radixNum).toFixed(2)
          ).toLocaleString("en-US"),
          payOrdrAmtPct: "15.54%",
          payOrdrAmtPctRised: false,
          payOrdrAmtIsPercent: true,
          // 支付买家数
          payOrdrUsrCnt: Number(
            Number(56981 * radixNum).toFixed(0)
          ).toLocaleString("en-US"),
          payOrdrUsrCntPct: "18.09%",
          payOrdrUsrCntPctRised: false,
          payOrdrUsrCntIsPercent: true,
          // 支付客单价
          payOrdrAup: "7.63",
          payOrdrAupPct: "3.11%",
          payOrdrAupPctRised: true,
          payOrdrAupIsPercent: true,
          // 支付转化率
          payUvRto: "8.82%",
          payUvRtoPct: "31.09%",
          payUvRtoPctRised: false,
          payUvRtoIsPercent: true,
          // 店铺关注用户数
          mallFavCnt: Number(Number(639).toFixed(0)).toLocaleString("en-US"),
          mallFavCntPct: "10.17%",
          mallFavCntPctRised: true,
          mallFavCntIsPercent: true,
          // 支付订单数
          payOrdrCnt: Number(
            Number(64972 * radixNum).toFixed(0)
          ).toLocaleString("en-US"),
          payOrdrCntPct: "18.77%",
          payOrdrCntPctRised: false,
          payOrdrCntIsPercent: true,
          // 老买家占比
          rpayOrdrUsrRto: "-",
          rpayOrdrUsrRtoPct: "-",
          rpayOrdrUsrRtoPctRised: false,
          rpayOrdrUsrRtoIsPercent: true,
        });
        var blob = new Blob([JSON.stringify(res, null, 2)], {
          type: "application/json",
        });
        var init = { status: 200 };
        return new Response(blob, init);
      }
    });
  };

  const pddMockAutoMatchFunc = (url, ...params) => {
    if (url.indexOf("/api/mallInfo/mallPayList") > -1) {
      return mockQueryMallDataPagePayList(url, ...params);
    }
    if (url.indexOf("/api/mallInfo/queryMallDataPageOverViewList") > -1) {
      return mockQueryMallDataPageOverviewList(url, ...params);
    }
    if (url.indexOf("/api/mallInfo/queryMallDataPageOverView") > -1) {
      return mockQueryMallDataPageOverview(url, ...params);
    }
    return originFetch(url, ...params);
  };

  unsafeWindow.fetch = (url, ...rest) => {
    if (!!pddMockSwitch) {
      return pddMockAutoMatchFunc(url, ...rest);
    } else {
      return originFetch(url, ...rest);
    }
  };
})();
